package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.achievement.service.DiaryService;
import com.lanou3g.ptp.api.resolver.annotation.SsoStudent;
import com.lanou3g.ptp.entity.Comment;
import com.lanou3g.ptp.entity.Diary;
import com.lanou3g.ptp.entity.Like;
import com.lanou3g.ptp.entity.Student;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import com.lanou3g.ptp.vo.AlertBody;
import com.lanou3g.ptp.vo.DiaryCalendarResult;
import com.lanou3g.ptp.vo.DiaryDetails;
import com.lanou3g.ptp.vo.DiaryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(description = "日志接口")
@RestController
public class DiaryController {

    @Reference
    private DiaryService diaryService;

    @ApiOperation("实习圈列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskIds", value = "任务ID"),
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "显示条数"),
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @GetMapping("/task/{taskIds}/diaries")
    public PtpResult diariesInTask(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                   @RequestParam(value = "size", defaultValue = "10") Integer size,
                                   @PathVariable Long[] taskIds,
                                   @ApiIgnore @SsoStudent Student student) {
        try {
            if (page <= 0) {
                page = 1;
            }
            PageInfo<Diary> p = diaryService.findDiariesWithLikesAndCommentsByTaskIds(taskIds, page, size);
            for (Diary diary : p.getList()) {
                for (Like like : diary.getLikes()) {
                    boolean isLiked = like.getStudentId().equals(student.getId());
                    diary.setIsLiked(isLiked);
                }
                boolean isMine = student.getId().equals(diary.getStudentId());
                diary.setIsMine(isMine);
            }
            return PtpResultUtils.ok(p);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("通过id查询日志信息")
    @GetMapping("/diary/{diaryId}")
    public PtpResult findDiaryById(@PathVariable Long diaryId) {
        Diary diary = this.diaryService.findDiaryById(diaryId);
        return PtpResultUtils.ok(diary);
    }

    @ApiOperation("学生添加日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
          @PostMapping("/diary")
        public PtpResult postMyDiary(@RequestBody Diary diary,
                @ApiIgnore @SsoStudent Student student) {
            diary.setStudentId(student.getId());
            try {
                boolean success = diaryService.insertDiary(diary);

                return success ? PtpResultUtils.ok() : PtpResultUtils.error(420, "日志添加失败");
            } catch (Exception e) {
                e.printStackTrace();
                return PtpResultUtils.error(500);
            }

    }


    @ApiOperation("学生通过id删除日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "diaryId", value = "日志id")
    })
    @DeleteMapping("/diary/{diaryId}")
    public PtpResult deleteMyDiary(@PathVariable Long diaryId,
                                   @ApiIgnore @SsoStudent Student student) {
        Diary diary = new Diary();
        diary.setId(diaryId);
        diary.setStudentId(student.getId());
        try {
            boolean success = diaryService.delete(diary);
            return success ? PtpResultUtils.ok() : PtpResultUtils.error(421, "删除日志失败");
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }

    @ApiOperation("批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "多个日志id")
    })
//    @DeleteMapping("/diaries/{ids}")
    public PtpResult deleteDiaryIds(@PathVariable Long[] ids) {

        try {
            diaryService.deleteDiaryIds(ids);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }


    @ApiOperation("学生点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "diaryId", value = "日志id"),
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @PostMapping("/diary/{diaryId}/like")
    public PtpResult like(@PathVariable Long diaryId,
                          @ApiIgnore @SsoStudent Student student) {
        Like like = new Like();
        like.setDiaryId(diaryId);
        like.setStudentId(student.getId());
        try {
            boolean success = diaryService.like(like);
            return success ? PtpResultUtils.ok() : PtpResultUtils.error(422, "点赞失败");
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("学生取消点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "diaryId", value = "日志id"),
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @DeleteMapping("/diary/{diaryId}/unlike")
    public PtpResult unlike(@PathVariable Long diaryId,
                            @ApiIgnore @SsoStudent Student student) {
        Like like = new Like();
        like.setDiaryId(diaryId);
        like.setStudentId(student.getId());
        try {
            boolean success = diaryService.unlike(like);
            return success ? PtpResultUtils.ok() : PtpResultUtils.error(423, "取消点赞失败");
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("学生添加评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @PostMapping("/comment")
    public PtpResult postComment(@RequestBody Comment comment,
                                 @ApiIgnore @SsoStudent Student student) {
        comment.setStudentId(student.getId());
        try {
            boolean success = diaryService.postComment(comment);
            return success ? PtpResultUtils.ok() : PtpResultUtils.error(424, "评论失败");
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("学生删除评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论id"),
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @DeleteMapping("/comment/{commentId}")
    public PtpResult deleteComment(@PathVariable Long commentId,
                                   @ApiIgnore @SsoStudent Student student) {
        try {
            boolean success = diaryService.deleteComment(commentId, student.getId());
            return success ? PtpResultUtils.ok() : PtpResultUtils.error(425, "删除评论失败");
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

//    @ApiOperation("学生按日志id查询评论")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "DiaryId")
//    })
//    @GetMapping("/myComment/{id}")
//    public PtpResult commentFindByDiaryId(@PathVariable Long id) {
//        try {
//            List<Comment> comments = commentService.find(id);
//            return PtpResultUtils.ok(comments);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return PtpResultUtils.error(500);
//        }
//    }

    @ApiOperation("学生查询所有日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @GetMapping("/myDiarys")
    public PtpResult myFindAll(@ApiIgnore @SsoStudent Student student) {
        try {
            List<Diary> diaries = diaryService.MyFindAll(student.getId());
            return PtpResultUtils.ok(diaries);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }

    @ApiOperation("根据教学环节id查询所有学生的日志概况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teachlinkId", value = "教学环节id")
    })
    @GetMapping("/teachlink/{teachlinkId}/diaries")
    public PtpResult diaryDetails(@PathVariable Long teachlinkId) {
        DiaryDetails details = this.diaryService.findDiaryDetailsByTeachlinkId(teachlinkId);
        return PtpResultUtils.ok(details);
    }

    @ApiOperation("根据任务id查询某个学生的日志概况")
    @GetMapping("/task/{taskId}/student/{studentId}/diaries")
    public PtpResult diaryDetails(@PathVariable Long taskId, @PathVariable Long studentId) {
        List<DiaryCalendarResult> diaryCalendar = this.diaryService.findDiaryCalendar(taskId, studentId);
        return PtpResultUtils.ok(diaryCalendar);
    }

    @ApiOperation("提醒某个学生上传某个任务下的某天的日志")
    @PostMapping("/diary/alert")
    public PtpResult alertDiary(@RequestBody AlertBody alert) {
        boolean b = this.diaryService.alertDiary(alert);
        // TODO: 2019/8/29 阶段报告上传提醒
        return b?PtpResultUtils.ok():PtpResultUtils.error(505,"发送失败");
    }

    @ApiOperation("学生补写日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @PostMapping("/diary/writeUp")
    public PtpResult writeUpDiary(@RequestBody Diary diary){
        boolean b = this.diaryService.writeUpDiary(diary);
        return b?PtpResultUtils.ok():PtpResultUtils.error(505,"补写失败");
    }


}
